package com.heaboy.shiwu;

public class TransactionTest {
    public static void main(String[] args) {
        // 配置数据库连接
        String url = "jdbc:mysql://localhost:3306/users?useSSL=false&serverTimezone=UTC";
        String username = "root";
        String password = "root";

        // 创建事务管理器
        TransactionManager transactionManager = new JdbcTransactionManager(url, username, password);

        // 创建代理工厂
        TransactionProxy transactionProxy = new TransactionProxy(transactionManager);

        // JDK动态代理
        UserService userService = new UserServiceImpl(transactionManager);
        UserService userProxy = transactionProxy.createProxy(userService);

        // CGLIB代理
        ProductService productService = new ProductService(transactionManager);
        ProductService productProxy = transactionProxy.createProxy(productService);

        try {
            System.out.println("===== 测试JDK动态代理 =====");
            userProxy.saveUser();
            userProxy.queryUser();

            System.out.println("\n===== 测试CGLIB代理 =====");
            productProxy.saveUser();
            productProxy.queryUser();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}